package vn.com.vega.music.view.custom;

import vn.com.vega.chacha.R;
import vn.com.vega.chacha.v2.ActivityBase;
import vn.com.vega.chacha.v2.SearchScreen;
import vn.com.vega.chacha.v2.SearchScreen.SearchListener;
import vn.com.vega.chacha.v2.SettingAccount;
import vn.com.vega.chacha.v2.receiver.NetworkStatusReceiver;
import vn.com.vega.music.utils.Const;
import vn.com.vega.music.utils.Util;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class TopTaskbarView extends RelativeLayout implements
		View.OnClickListener, Const {
	private Context mContext;
	private ImageButton mBackBtn;
	private LinearLayout mLayoutBack;
	private ImageButton mPersonalBtn;
	private ImageButton mSearchBtn;
	private TextBox mSearchBox;
	private TextView mTitle;
	private View mLayoutmainMenu;
	private LinearLayout mLayoutMenuExtract;
	private InputMethodManager mImm;
	private SearchListener searchListener;

	public TopTaskbarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		mContext = context;
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.view_top_task_bar, this);
		mBackBtn = (ImageButton) view.findViewById(R.id.bt_back);
		mLayoutBack = (LinearLayout) view.findViewById(R.id.layout_bt_back);
		mPersonalBtn = (ImageButton) view.findViewById(R.id.bt_goto_personal);
		mSearchBtn = (ImageButton) view.findViewById(R.id.bt_search);
		mSearchBox = (TextBox) view.findViewById(R.id.textBox_main_task);
		mSearchBox.setIsSearchBox(true);
		mTitle = (TextView) view.findViewById(R.id.tv_title);
		mLayoutmainMenu = findViewById(R.id.layout_menuitem);
		mLayoutMenuExtract = (LinearLayout) findViewById(R.id.layout_menu_extract);

		mImm = (InputMethodManager) mContext
				.getSystemService(Context.INPUT_METHOD_SERVICE);

		mSearchBtn.setOnClickListener(this);
		mPersonalBtn.setOnClickListener(this);
		mBackBtn.setOnClickListener(this);
		mLayoutBack.setOnClickListener(this);
		mSearchBox.setOnKeyListener(searchBoxOnKeyListener);
	}

	public void showMainMenu(boolean show) {
		if (show) {
			mLayoutmainMenu.setVisibility(View.VISIBLE);
		} else {
			mLayoutmainMenu.setVisibility(View.GONE);
		}
	}

	public void addExtractMenu(int buttonDrawableId,
			View.OnClickListener clickListener, String menuTag) {
		ImageButton imageButton = new ImageButton(mContext);
		imageButton.setOnClickListener(clickListener);
		imageButton.setTag(menuTag);
		imageButton.setBackgroundColor(Color.TRANSPARENT);
		imageButton.setImageResource(buttonDrawableId);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				(int) mContext.getResources().getDimension(
						R.dimen.top_menu_width),
				android.widget.LinearLayout.LayoutParams.MATCH_PARENT);
		lp.gravity = Gravity.CENTER;
		mLayoutMenuExtract.addView(imageButton, lp);
		if (!mLayoutMenuExtract.isShown()) {
			mLayoutMenuExtract.setVisibility(View.VISIBLE);
		}
	}

	public View getExtractMenu(String menuTag) {
		View menu = mLayoutMenuExtract.findViewWithTag(menuTag);
		return menu;
	}

	public void changeToTitleOnly(String title) {
		mTitle.setText(title);
		mLayoutmainMenu.setVisibility(View.GONE);
		mTitle.setVisibility(View.VISIBLE);
	}

	public void showBackButton() {
		mBackBtn.setImageResource(R.drawable.bt_back);
		mBackBtn.setImageResource(R.drawable.bt_back);
		View.OnClickListener onClickListener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				((Activity) mContext).finish();
			}
		};
		mBackBtn.setOnClickListener(onClickListener);
		mLayoutBack.setOnClickListener(onClickListener);
	}

	// public void changeToPlayerScreen() {
	// mBackBtn.setImageResource(R.drawable.bt_back);
	// View.OnClickListener onClickListener = new OnClickListener() {
	//
	// @Override
	// public void onClick(View v) {
	// // TODO Auto-generated method stub
	// ((Activity) mContext).finish();
	// }
	// };
	// mBackBtn.setOnClickListener(onClickListener);
	// mLayoutBack.setOnClickListener(onClickListener);
	// }

	public View getLayoutBack() {
		return mLayoutBack;
	}

	public View getRealButtonBack() {
		return mBackBtn;
	}

	public TextBox getSearchBox() {
		return mSearchBox;
	}

	public void changeToSearchScreen() {
		mPersonalBtn.setVisibility(View.GONE);
		mSearchBox.setVisibility(View.VISIBLE);
		mSearchBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				doSearch();
			}
		});
	}

	public void setOnSearchListener(SearchListener listener) {
		searchListener = listener;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.bt_goto_personal:
			if (!NetworkStatusReceiver.isConnected()) {
				if (mContext instanceof ActivityBase) {
					ActivityBase act = (ActivityBase) mContext;
					act.showWarningNotConnectInternet();
				}
				return;
			}
			Intent intent = new Intent(mContext, SettingAccount.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			mContext.startActivity(intent);
			break;
		case R.id.bt_search:
			if (!NetworkStatusReceiver.isConnected()) {
				if (mContext instanceof ActivityBase) {
					ActivityBase act = (ActivityBase) mContext;
					act.showWarningNotConnectInternet();
				}
				return;
			}
			if (mSearchBox.isShown()) {
				
				if (Util.checkStringIsemptyOrNull(mSearchBox.getText())) {
					hideSearchBox();
				} else {
					doSearch();
				}
			} else {
				showSearchBox();
			}
			break;
		case R.id.layout_bt_back:
		case R.id.bt_back:
			((Activity) mContext).finish();
			break;

		default:
			break;
		}
	}

	public boolean isShowSearchBox() {
		return mSearchBox.isShown();
	}

	public void setSearchBoxOnKeyListener(View.OnKeyListener onKeyListener) {
		mSearchBox.setOnKeyListener(onKeyListener);
	}

	//
	//
	public void showSearchBox() {
		mPersonalBtn.setVisibility(View.GONE);
		mSearchBox.setVisibility(View.VISIBLE);
		mSearchBox.requestFocus();
		showSoftInPut();
	}

	public void hideSearchBox() {
		mPersonalBtn.setVisibility(View.VISIBLE);
		mSearchBox.setVisibility(View.GONE);
		mSearchBox.setText("");
		hideSoftInput();
	}

	public void showSoftInPut() {
		try {
			mImm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public void hideSoftInput() {
		try {
			mImm.hideSoftInputFromWindow(mSearchBox.getWindowToken(), 0);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	//
	private OnKeyListener searchBoxOnKeyListener = new View.OnKeyListener() {

		@Override
		public boolean onKey(View v, int keyCode, KeyEvent event) {
			// TODO Auto-generated method stub
			if ((event.getAction() == KeyEvent.ACTION_UP)
					&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
				doSearch();
				return true;
			}

			return false;
		}
	};

	private void doSearch() {
		String keySearch = mSearchBox.getText().toString();
		if (Util.checkStringIsemptyOrNull(keySearch)) {
			return;
		}

		if (searchListener != null) {
			searchListener.onSearch(keySearch);
			return;
		}

		// Default if not set searchListener
		Intent search = new Intent(mContext, SearchScreen.class);
		search.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		search.putExtra(SearchScreen.KEY_SEARCH, keySearch);
		mContext.startActivity(search);
		hideSearchBox();
	}
	//
	// private void gotoPersonal() {
	// Intent personal = new Intent(mContext, PersonalActivity.class);
	// personal.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	// mContext.startActivity(personal);
	// }
	//
	// private void gotoLoginActivity() {
	// Intent login = new Intent(mContext, LoginActivity.class);
	// ((Activity) mContext).startActivityForResult(login,
	// REQUEST_CODE_LOGIN_GO_PERSONAL);
	// }
	//
	// TextWatcher onSearchTextChange = new TextWatcher() {
	//
	// @Override
	// public void onTextChanged(CharSequence s, int start, int before, int
	// count) {
	// // TODO Auto-generated method stub
	// if (s.length() > 0) {
	// mCleanTextBtn.setVisibility(View.VISIBLE);
	// mSearchIconInBox.setVisibility(View.INVISIBLE);
	// } else {
	// mCleanTextBtn.setVisibility(View.INVISIBLE);
	// mSearchIconInBox.setVisibility(View.VISIBLE);
	// }
	// }
	//
	// @Override
	// public void beforeTextChanged(CharSequence s, int start, int count,
	// int after) {
	// // TODO Auto-generated method stub
	//
	// }
	//
	// @Override
	// public void afterTextChanged(Editable s) {
	// // TODO Auto-generated method stub
	//
	// }
	// };
	//
	// // View.OnFocusChangeListener searchBoxForcusChangeListener = new
	// View.OnFocusChangeListener() {
	// //
	// // @Override
	// // public void onFocusChange(View v, boolean hasFocus) {
	// // // TODO Auto-generated method stub
	// // if (!hasFocus) {
	// // hideSearchBox();
	// // }
	// // }
	// // };
	//
	// // static class Login extends AsyncTask<Void, Void, Boolean> {
	// // WeakReference<TopTaskbarView> toptask;
	// // User user;
	// // ProgressDialog progress;
	// //
	// // public Login(TopTaskbarView toptask, User us) {
	// // this.toptask = new WeakReference<TopTaskbarView>(toptask);
	// // user = us;
	// // progress = new ProgressDialog(toptask.mContext);
	// // progress.setMessage(toptask.mContext.getString(R.string.logging_in));
	// // }
	// //
	// // @Override
	// // protected Boolean doInBackground(Void... params) {
	// // // TODO Auto-generated method stub
	// // if (user.login()) {
	// //
	// // }
	// // return null;
	// // }
	// //
	// // @Override
	// // protected void onPostExecute(Boolean result) {
	// // // TODO Auto-generated method stub
	// // super.onPostExecute(result);
	// // }
	// //
	// // @Override
	// // protected void onPreExecute() {
	// // // TODO Auto-generated method stub
	// // super.onPreExecute();
	// // }
	// //
	// // }
	//
	// View.OnClickListener shareClickListener = new View.OnClickListener() {
	//
	// @Override
	// public void onClick(View v) {
	// // TODO Auto-generated method stub
	// AppInstantClipVN app =
	// (AppInstantClipVN)mContext.getApplicationContext();
	// if (app.videoCurrentPlay == null && app.videoNeedPlay == null) {
	// return;
	// }
	// Video video = (app.videoCurrentPlay == null)? app.videoNeedPlay :
	// app.videoCurrentPlay;
	// Intent i=new Intent(android.content.Intent.ACTION_SEND);
	// i.setType("text/plain");
	// i.putExtra(Intent.EXTRA_TEXT, video.sharing_url);
	// mContext.startActivity(Intent.createChooser(i, "Chia sẻ bằng"));
	// }
	// };
	//
	// public boolean searchViewIsShow() {
	// return mLayoutShowSearchView.isShown();
	// }

}
